# -*- coding: utf-8 -*-
# Author: xiantingDeng
# Email: dengxianting@shxgroup.net
# Time: 2020/8/6 17:23

import pytest
import allure
from appium import webdriver

from common.element.element import Element
from common.utils import config
from common.utils.log_util import TNLog
from common.utils.file_util import get_img_file_path
# 屏幕截图专用driver
screenshot_driver = None


@pytest.fixture(scope="session")
def log():
    # 生成全局log日志工具
    global log
    log = TNLog()
    return log
#
#
# # 测试报告失败截图  allure失败截图
# @pytest.mark.hookwrapper
# def pytest_runtest_makereport(item):
#     outcome = yield
#     report = outcome.get_result()
#     report.description = str(item.function.__doc__)
#
#     if report.when == 'call' or report.when == "setup":
#         xfail = hasattr(report, 'wasxfail')
#         if (report.skipped and xfail) or (report.failed and not xfail):
#             # img_name = get_img_file_path("用例失败截图")
#             img_name = screenshot_driver.screenshot("用例失败截图")
#             with open(img_name, mode="rb") as f:
#                 file = f.read()
#             allure.attach(file, "失败截图", allure.attachment_type.PNG)


@pytest.fixture(scope="session")
def driver(log):
    log.info("本次测试报告路径：{}")
    log.info("读取配置信息：{}".format(config.appium_config))
    try:
        log.info("正在启动driver")
        app_driver = webdriver.Remote(config.host, config.appium_config)
        log.info("当前运行环境是：{}".format(config.apkVersion))
        global screenshot_driver
        screenshot_driver = app_driver
        app_driver.implicitly_wait(2)
        return Element(app_driver, log)
    except Exception as e:
        log.warning("driver启动失败，失败原因：{}".format(e))
        raise
